package cn.enilu.flash.api.controller.park;

import cn.enilu.flash.bean.constant.factory.PageFactory;
import cn.enilu.flash.bean.core.BussinessLog;
import cn.enilu.flash.bean.dictmap.CommonDict;
import cn.enilu.flash.bean.entity.park.ParkingInfo;
import cn.enilu.flash.bean.enumeration.BizExceptionEnum;
import cn.enilu.flash.bean.exception.GunsException;
import cn.enilu.flash.bean.vo.front.Rets;
import cn.enilu.flash.service.park.ParkingInfoService;
import cn.enilu.flash.utils.BeanUtil;
import cn.enilu.flash.utils.ToolUtil;
import cn.enilu.flash.utils.factory.Page;
import cn.enilu.flash.warpper.ParkInfoWarpper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/parkinfo")
public class ParkingInfoController {
	private  Logger logger = LoggerFactory.getLogger(getClass());
	@Autowired
	private ParkingInfoService parkingInfoService;

	@RequestMapping(value = "/list",method = RequestMethod.GET)
	public Object list() {
		Page<ParkingInfo> page = new PageFactory<ParkingInfo>().defaultPage();
		page = parkingInfoService.queryPage(page);
		List list = (List) new ParkInfoWarpper(BeanUtil.objectsToMaps(page.getRecords())).warp();
		page.setRecords(list);
		return Rets.success(page);
	}
	@RequestMapping(method = RequestMethod.POST)
	@BussinessLog(value = "编辑车位信息", key = "name",dict= CommonDict.class)
	public Object save(@ModelAttribute ParkingInfo parkingInfo){
		if(parkingInfo.getId()==null){
			parkingInfoService.insert(parkingInfo);
		}else {
			parkingInfoService.update(parkingInfo);
		}
		return Rets.success();
	}
	@RequestMapping(method = RequestMethod.DELETE)
	@BussinessLog(value = "删除车位信息", key = "id",dict= CommonDict.class)
	public Object remove(Long id){
		if (ToolUtil.isEmpty(id)) {
			throw new GunsException(BizExceptionEnum.REQUEST_NULL);
		}
		parkingInfoService.delete(id);
		return Rets.success();
	}

	@RequestMapping(value = "/get/{id}", method = RequestMethod.GET)
	public Object get(@PathVariable("id") Long id){
		if(id != null && id > 0){
			ParkingInfo info = parkingInfoService.get(id);
			if(info == null){
				return Rets.failure("车位信息不存在");
			}else{
				return Rets.success(new ParkInfoWarpper(BeanUtil.beanToMap(info)).warp());
			}
		}else{
			return Rets.failure("车位信息不存在");
		}
	}

	@RequestMapping(value = "/getParkings/{communityId}", method = RequestMethod.GET)
	public Object getParkings(@PathVariable("communityId") Long communityId){
		if(communityId != null && communityId > 0){
			List<Map<String,Object>> list = parkingInfoService.getParkings(communityId);
			if(list == null){
				throw new GunsException(BizExceptionEnum.REQUEST_NULL);
			}else{
				return Rets.success(list);
			}
		}else{
			throw new GunsException(BizExceptionEnum.REQUEST_NULL);
		}
	}
}